package com.upper.service.ipself;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.lucksyc.utilindependent.resultview.core.data.ViewData;
import org.lucksyc.utilindependent.resultview.seed.deal.ViewBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bean.Computer;
import com.bean.ComputerSelf;
import com.componentbean.UserNginxSet;
import com.model.ComputerIpModel;
import com.util.Util;

@Service
public class IpSelfInterfaceImpl implements IpSelfInterface {
	private static final Logger logger = LoggerFactory.getLogger(IpSelfInterfaceImpl.class);

	@Autowired
	ViewBuilder viewBuilder;

	@Autowired
	UserNginxSet userset;

	@Override
	public ViewData getMyselfList() {
		ComputerSelf myself = userset.getComputerSelf();
		List<ComputerIpModel> list = new ArrayList<>();
		ComputerIpModel model = new ComputerIpModel();
		model.setId(String.valueOf(myself.getMySelfDefault().getId()));
		model.setIp(myself.getMySelfDefault().getIp());
		list.add(model);
		if (null != myself.getOther()) {
			Iterator<Entry<Integer, Computer>> iterator = myself.getOther().entrySet().iterator();
			while (iterator.hasNext()) {
				Computer e = iterator.next().getValue();
				ComputerIpModel m = new ComputerIpModel();
				m.setId(String.valueOf(e.getId()));
				m.setIp(e.getIp());
				list.add(m);
			}
		}
		return viewBuilder.wrapPage(list, list.size());
	}

	@Override
	public ViewData addMyself(String ip) {
		if (Util.checkIP(ip)) {
			ComputerSelf myself = userset.getComputerSelf();
			if (myself.getIpSet().add(ip)) {
				Integer id = myself.nextId();
				boolean existsId = true;
				for (int i = 0; i < 1000; i++) {
					if (myself.getOther().containsKey(id)) {
						id = myself.nextId();
					} else {
						existsId = false;
						break;
					}
				}
				if (existsId) {
					return viewBuilder.wrapError("数据标识出错！", 10101);
				}
				Computer c = new Computer(id, "local", "local", ip);
				userset.getComputerSelf().getOther().put(id, c);
				logger.info("IpSelfInterfaceImpl:addMyself:add:" + ip);
				return viewBuilder.wrap(true);
			} else {
				return viewBuilder.wrapError("IP已经存在！", 10102);
			}
		} else {
			return viewBuilder.wrapError("IP不合法！", 10102);
		}
	}

	@Override
	public ViewData delMyself(String id) {
		if (Util.isNegtiveInteger(id)) {
			Integer cid = Integer.parseInt(id.trim());
			if (userset.getComputerSelf().getOther().containsKey(cid)) {
				if (userset.getServiceList().isUsing(cid)) {
					return viewBuilder.wrapError("数据正在使用之中", 20401);
				} else {
					Computer c = userset.getComputerSelf().getOther().get(cid);
					userset.getComputerSelf().getIpSet().remove(c.getIp());
					userset.getComputerSelf().getOther().remove(cid);
					logger.info("IpSelfInterfaceImpl:delMyself:del:" + id + " " + c.getIp());
					return viewBuilder.wrap(true);
				}
			} else {
				return viewBuilder.wrapError("不存在的数据", 20420);
			}
		} else {
			return viewBuilder.wrapError("数据不合法", 20450);
		}
	}

}
